package com.plumecache.core.interceptor;

import com.plumecache.core.CacheService;
import com.plumecache.core.annotation.Version;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author anson.yin
 * @date 2021/11/16 3:19 PM
 */
public class VersionCacheInterceptor extends BaseCacheInterceptor {
    @Override
    public boolean preHandle(CacheService target, Method method, Object[] args, Map<String, Object> context) {
        Object value = getValue(method, args);

        if (null == value) {
            return true;
        }

        if (!value.getClass().isAnnotationPresent(Version.class)) {
            return true;
        }

        String version = value.getClass().getAnnotation(Version.class).value();

        if (StringUtils.isBlank(version)) {
            return true;
        }

        String newKey = getKey(method, args).concat("@").concat(version);
        setKey(method, args, newKey);
        return true;
    }


}
